package com.isdepci.project.carbon.service.impl;

import com.isdepci.framework.web.domain.AjaxResult;
import com.isdepci.project.carbon.domain.BNitreEmission;
import com.isdepci.project.carbon.domain.param.WeatherDataExcel;
import com.isdepci.project.carbon.mapper.BNitreEmissionMapper;
import com.isdepci.project.carbon.service.IBNitreEmissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * 脱硝排放Service业务层处理
 *
 * @author isdepci
 * @date 2025-03-29
 */
@Service
public class BNitreEmissionServiceImpl implements IBNitreEmissionService
{
    @Autowired
    private BNitreEmissionMapper bNitreEmissionMapper;



    @Override
    public AjaxResult calNitreEmission(Long projectId) {
        // 查询出所有的BNitreEmission数据
        List<BNitreEmission> bNitreEmissions = bNitreEmissionMapper.selectBNitreEmissionByProjectId(projectId);
        // 初始化总的排放量
        BigDecimal totalEmission = BigDecimal.ZERO;

        // 遍历每一条数据，计算排放量
        for (BNitreEmission bNitreEmission : bNitreEmissions) {
            // 计算排放量 EF = consumption * emissionFactor
            BigDecimal emission = bNitreEmission.getConsumption()
                    .multiply(bNitreEmission.getEmissionFactor());

            bNitreEmission.setCarbonEmissions(emission);
            bNitreEmissionMapper.updateBNitreEmission(bNitreEmission);
            // 累加排放量
            totalEmission = totalEmission.add(emission);
        }
        // 返回总排放量
        return AjaxResult.success(totalEmission.setScale(2, RoundingMode.HALF_UP));
    }

    public AjaxResult changeDataByProjectId(WeatherDataExcel excel) {
        List<BNitreEmission> list =  excel.getNitreEmissionList();
        list.forEach(item->{
            if(excel.getProjectId()!=null){
                item.setProjectId(excel.getProjectId());
            }
        });
        if(list.size()>0){
            bNitreEmissionMapper.insertList(list);
        }
        return AjaxResult.success();
    }


    /**
     * 查询脱硝排放
     */
    @Override
    public BNitreEmission selectBNitreEmissionById(Long id)
    {
        return bNitreEmissionMapper.selectBNitreEmissionById(id);
    }

    /**
     * 查询脱硝排放列表
     */
    @Override
    public List<BNitreEmission> selectBNitreEmissionList(BNitreEmission bNitreEmission)
    {
        return bNitreEmissionMapper.selectBNitreEmissionList(bNitreEmission);
    }

    /**
     * 新增脱硝排放
     */
    @Override
    public int insertBNitreEmission(BNitreEmission bNitreEmission)
    {
        return bNitreEmissionMapper.insertBNitreEmission(bNitreEmission);
    }

    /**
     * 修改脱硝排放
     */
    @Override
    public int updateBNitreEmission(BNitreEmission bNitreEmission)
    {
        return bNitreEmissionMapper.updateBNitreEmission(bNitreEmission);
    }

    /**
     * 批量删除脱硝排放
     */
    @Override
    public int deleteBNitreEmissionByIds(Long[] ids)
    {
        return bNitreEmissionMapper.deleteBNitreEmissionByIds(ids);
    }

    /**
     * 删除脱硝排放信息
     */
    @Override
    public int deleteBNitreEmissionById(Long id)
    {
        return bNitreEmissionMapper.deleteBNitreEmissionById(id);
    }


}
